package es.indra.malaga.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import es.indra.malaga.dao.IAbstractDAO;

public abstract class AbstractDAOImpl<T> implements IAbstractDAO<T>,Serializable{

	private static final long serialVersionUID = -7171323343265207076L;

	@Autowired
	protected HibernateTemplate hibernateTemplate;

	@SuppressWarnings("unchecked")
	private Class<T> tipo = (Class<T>) ((ParameterizedType) getClass()
			.getGenericSuperclass()).getActualTypeArguments()[0];

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public void insertar(T obj) {
		hibernateTemplate.save(obj);
	}

	@Override
	public void modificar(T obj) {
		hibernateTemplate.update(obj);
	}

	@Override
	public void borrar(T obj) {
		hibernateTemplate.delete(obj);
	}

	@Override
	public T buscar(Serializable id) {
		return (T) hibernateTemplate.get(tipo, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> listarTodos() {
		return hibernateTemplate.find("from " + tipo.getName());
	}

}
